package com.fzb.zrlog.simple;

import com.fzb.common.dao.impl.DAO;
import com.fzb.zrlog.simple.controller.QueryLogControl;
import com.fzb.zrlog.simple.controller.UserControl;
import com.fzb.zrlog.simple.incp.LoginInterceptor;
import com.hibegin.http.server.WebServerBuilder;
import com.hibegin.http.server.config.AbstractServerConfig;
import com.hibegin.http.server.config.RequestConfig;
import com.hibegin.http.server.config.ResponseConfig;
import com.hibegin.http.server.config.ServerConfig;
import com.hibegin.http.server.util.FreeMarkerUtil;
import com.hibegin.http.server.util.PathUtil;
import com.hibegin.http.server.web.MethodInterceptor;
import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;

import java.nio.charset.Charset;

/**
 * Created by xiaochun on 15-7-14.
 */
public class Application extends AbstractServerConfig {
    public static void main(String[] args) {
        // 启动 server
        System.out.println(Charset.defaultCharset());
        new WebServerBuilder.Builder().config(new Application()).build().startWithThread();
    }

    @Override
    public ServerConfig getServerConfig() {
        ServerConfig serverConfig = new ServerConfig();
        serverConfig.getRouter().addMapper("/admin", UserControl.class);
        serverConfig.getRouter().addMapper("/post", QueryLogControl.class);
        serverConfig.getRouter().addMapper("", QueryLogControl.class);
        serverConfig.addInterceptor(LoginInterceptor.class);
        serverConfig.addInterceptor(MethodInterceptor.class);
        try {
            FreeMarkerUtil.init(PathUtil.getStaticPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
        SQLiteDataSource ds = new SQLiteDataSource();
        ds.setUrl("jdbc:sqlite:" + PathUtil.getRootPath() + "/db/zrlog.db");
        ds.getConfig().setEncoding(SQLiteConfig.Encoding.UTF8);
        DAO.setDs(ds);
        return serverConfig;
    }

    @Override
    public RequestConfig getRequestConfig() {
        return null;
    }

    @Override
    public ResponseConfig getResponseConfig() {
        return null;
    }
}
